import os
import random
import qrcode
from django.utils import timezone
from django.db import models
from django.conf import settings
# Create your models here.
PORIRAIT_DEFAULT_PATH = 'news/images/default_portrait.jpeg'


def user_upload_to(instance, filename):
    ext = os.path.splitext(filename)[1]
    fn = "%s_%s%s" % (timezone.now().strftime("%Y%m%d%H%M%S"),
                      random.randint(100000, 999999), ext)
    return fn


class User(models.Model):
    vip_choices = (('0', 'Vip0'),
                   ('1', 'Vip1'),
                   ('2', 'Vip2'),
                   ('3', 'Vip3'))
    phone = models.CharField(max_length=20, unique=True)
    nickname = models.CharField(max_length=20, default='')
    password = models.CharField(max_length=16)
    portrait = models.ImageField(upload_to=user_upload_to, default="")
    integral = models.CharField(max_length=256, default=0)
    number = models.CharField(max_length=256, default=0)
    vip = models.CharField(max_length=1, choices=vip_choices, default=0)

    def __str__(self):
        return self.nickname

    class Meta:
        db_table = 'author'

    def portrait_url(self):
        if self.portrait:
            return self.portrait.url
        return 'images/head.png'


class UserLogin(models.Model):
    User_id = models.ForeignKey(User, on_delete=models.CASCADE)
    login_time = models.DateTimeField(default=timezone.now)
    logout_time = models.DateTimeField(default=timezone.now)


class Article(models.Model):
    author = models.ForeignKey(User, on_delete=models.CASCADE, related_name='articles', verbose_name='作者', default=1)
    sort = models.CharField(max_length=4, default='')
    title = models.CharField(u'标题', max_length=50)
    source = models.CharField(max_length=20, null=True, verbose_name='来源')
    abstract = models.CharField(max_length=100, verbose_name='摘要')
    images = models.ImageField(upload_to=user_upload_to, default="")
    content = models.TextField(u'内容')
    view_number = models.PositiveIntegerField(default=0, verbose_name='点击量')
    share_number = models.PositiveIntegerField(default=0, verbose_name='分享量')
    like_number = models.PositiveIntegerField(default=0, verbose_name='点赞量')
    QR_code = models.CharField(u'二维码', max_length=256, )
    pub_date = models.DateTimeField(u'发表时间', default=timezone.now)
    audit_choice = ((0, '待审核'),
                    (1, '通过审核'),
                    (2, '审核未通过'))
    audit_status = models.PositiveIntegerField(choices=audit_choice, default=0, verbose_name='审核状态')

    def __str__(self):
        return self.title


# 评论
class Comment(models.Model):
    author = models.ForeignKey(User, related_name='comments', on_delete=models.CASCADE, verbose_name='评论者')
    article = models.ForeignKey(Article, related_name='comment', on_delete=models.CASCADE, verbose_name='被评论的文章',default=0)
    content = models.CharField(max_length=100, verbose_name='评论内容')
    nickname = models.CharField(max_length=20, default='1')
    comment_time = models.DateTimeField(auto_now_add=True, verbose_name='评论时间')


# 消息表
class Message(models.Model):
    article = models.ForeignKey(Article, related_name='comments', on_delete=models.CASCADE, verbose_name='作者(谁的消息表)')
    content = models.CharField(max_length=100, verbose_name='内容')
    send_time = models.DateTimeField(auto_now_add=True, verbose_name='消息时间')


# 积分账单表
class Bill(models.Model):    #积分
    user = models.ForeignKey(User, on_delete=models.CASCADE, default='')
    time = models.DateTimeField(default=timezone.now)
    pays = models.CharField(max_length=10)
    money = models.DecimalField(max_digits=7, decimal_places=2)


class Charge(models.Model):  #充值费用
    pays_number = models.CharField(max_length=20)
    pays_time = models.DateTimeField(default=timezone.now)
    users = models.ForeignKey(User, on_delete=models.CASCADE, default='')
    pays_money = models.DecimalField(max_digits=7, decimal_places=2)


class Zan(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='zan_of_user')
    article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='zan_of_article')


class Read(models.Model):
    user = models.ForeignKey(User, on_delete=models.CASCADE, related_name='read_of_user')
    article = models.ForeignKey(Article, on_delete=models.CASCADE, related_name='read_of_article')


def make_code(text):
    #  version是二维码的尺寸，数字大小决定二维码的密度       error_correction：是指误差
    # box_size:参数用来控制二维码的每个单元(box)格有多少像素点
    # border: 参数用控制每条边有多少个单元格(默认值是4，这是规格的最小值
    qr = qrcode.QRCode(version=5,
                       error_correction=qrcode.constants.ERROR_CORRECT_L,
                       box_size=8,
                       border=4,
                       )
    # 添加数据
    qr.add_data(text)
    # 生成二维码
    qr.make(fit=True)
    img = qr.make_image()
    img.show()
    return img


if __name__ == '__main__':
    text = input("请输入你想说的话:")
    make_code(text)
# import os
# import random
# import qrcode
# from django.utils import timezone
# from django.db import models
# from django.conf import settings
# # Create your models here.
# PORIRAIT_DEFAULT_PATH = 'news/images/default_portrait.jpeg'
#
#
# def user_portrait_upload_to(instance, filename):
#     ext = os.path.splitext(filename)[1]
#     fn = "%s_%s%s" % (timezone.now().strftime("%Y%m%d%H%M%S"),
#                       random.randint(100000, 999999), ext)
#     return "portrait/%s" % fn
#
#
# class LoginUser(models.Model):
#     vip_choices = (('0', 'Vip0'),
#                    ('1', 'Vip1'),
#                    ('2', 'Vip2'),
#                    ('3', 'Vip3'))
#     phone = models.CharField(max_length=20)
#     nickname = models.CharField(max_length=20, default='')
#     password = models.CharField(max_length=16)
#     portrait = models.ImageField(upload_to=user_portrait_upload_to, default="static/news/images/default_portrait.jpeg")
#     integral = models.CharField(max_length=256, default='0')
#     free_number = models.CharField(max_length=256, default='0')
#     vip = models.CharField(max_length=1, choices=vip_choices, default='0')
#
#     def __str__(self):
#         return self.phone
#
#     def get_portrait_url(self):
#         if not self.portrait:
#             return settings.STATIC_URL + PORTRAIT_DEFAULT_PATH
#         return self.portrait.url
#
#
# class Article(models.Model):
#     title = models.CharField(u'标题', max_length=256)
#     content = models.TextField(u'内容')
#     ph = models.ImageField(u'图片', upload_to=user_portrait_upload_to)
#     QR_code = models.CharField(u'二维码', max_length=256, )
#     pub_date = models.DateTimeField(u'发表时间', default=timezone.now)
#     update_time = models.DateTimeField(u'更新时间', default=timezone.now)
#
#
# def make_code(text):
#     #  version是二维码的尺寸，数字大小决定二维码的密度       error_correction：是指误差
#     # box_size:参数用来控制二维码的每个单元(box)格有多少像素点
#     # border: 参数用控制每条边有多少个单元格(默认值是4，这是规格的最小值
#     qr = qrcode.QRCode(version=5,
#                        error_correction=qrcode.constants.ERROR_CORRECT_L,
#                        box_size=8,
#                        border=4,
#                        )
#     # 添加数据
#     qr.add_data(text)
#     # 生成二维码
#     qr.make(fit=True)
#     img = qr.make_image()
#     img.show()
#     return img
#
#
# if __name__ == '__main__':
#     text = input("请输入你想说的话:")
#     make_code(text)